package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.SetDefaultAddressRequestFilter;
import com.mmlc.network.responsebean.AddressListResponseBean;
import com.mmlc.network.responsebean.SetDefaultAddressResponseBean;
import com.mmlc.utils.ToastUtils;
import com.mmlc.utils.ViewUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/7/20.
 */
public class AddressAdapter extends BaseAdapter {
    private final LayoutInflater mInflater;
    List<AddressListResponseBean.DataBean> datas;
    private Context mContext;
    int selectedAddress = -1;

    public AddressAdapter(Context context,List<AddressListResponseBean.DataBean> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        this.datas = datas;


    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_information_select, parent, false);
            holder.tv_name = ViewUtils.getView(convertView, R.id.tv_name);
            holder.tv_phone = ViewUtils.getView(convertView, R.id.tv_phone);
            holder.tv_address = ViewUtils.getView(convertView, R.id.tv_address);
            holder.tv_setting = ViewUtils.getView(convertView, R.id.tv_setting);

            convertView.setTag(holder);
        } else holder = (ViewHolder) convertView.getTag();



        AddressListResponseBean.DataBean address = datas.get(position);
        if (!ViewUtils.isEmpty(address.getContactor())) {
            holder.tv_name.setText(address.getContactor());
        }
        if (!ViewUtils.isEmpty(address.getPhone())) {
            holder.tv_phone.setText(address.getPhone());
        }

        StringBuffer sb = new StringBuffer();
        if (!ViewUtils.isEmpty(address.getProvince_name()))
            sb.append(address.getProvince_name() + "/");
        if (!ViewUtils.isEmpty(address.getCity_name())) sb.append(address.getCity_name() + "/");
        if (!ViewUtils.isEmpty(address.getArea_name())) sb.append(address.getArea_name() + "/");
        if (!ViewUtils.isEmpty(address.getAddress())) sb.append(address.getAddress());
        holder.tv_address.setText(sb.toString());

        if (!ViewUtils.isEmpty(datas.get(position).getIs_default()) && "1".equals(datas.get(position).getIs_default())) {
            holder.tv_setting.setText("默认地址");
            holder.tv_setting.setBackgroundResource(R.drawable.shape_border_null_cccccc);
        } else {
            holder.tv_setting.setText("设为默认");
            holder.tv_setting.setBackgroundResource(R.drawable.shape_border_null_212121);
        }

        holder.tv_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < datas.size(); i++) {
                    if (!ViewUtils.isEmpty(datas.get(i).getIs_default()) && "1".equals(datas.get(i).getIs_default())) {
                        selectedAddress = i;
                        break;
                    }
                }
                datas.get(position).setIs_default("1");
                if (selectedAddress != -1 && selectedAddress != position && selectedAddress<=datas.size()-1)
                    datas.get(selectedAddress).setIs_default("0");
                selectedAddress = position;
                setDefaultAddress(position);
            }
        });

        return convertView;
    }

    private void setDefaultAddress(final int position) {
        SetDefaultAddressRequestFilter filter = new SetDefaultAddressRequestFilter((BaseParentActivity) mContext);
        filter.requestBean.aid = Integer.parseInt(datas.get(position).getId());
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<SetDefaultAddressResponseBean>() {
            @Override
            public void onSuccess(SetDefaultAddressResponseBean result) {
                if (result == null || result.getCode() != 0) {
                    setDefaultAddress(position);
                }else{
                    notifyDataSetChanged();
                }
            }
            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess.toString());

            }
        });
    }



    static class ViewHolder {
        TextView tv_name, tv_phone, tv_address,tv_setting;

    }
}


